package com.citasgoo.common.utils;

import java.util.*;

public class RandomNum {

    /**
     * 生成指定连续范围内的N个不重复随机数数组
     *
     * @param min
     * @param max
     * @param n
     * @return
     */
    public static Integer[] randomArray(Integer min, Integer max, Integer n) {
        Integer len = max - min + 1;

        if (max < min || n > len) {
            return null;
        }

        // 初始化给定范围的待选数组
        Integer[] source = new Integer[len];
        for (Integer i = min; i < min + len; i++) {
            source[i - min] = i;
        }

        Integer[] result = new Integer[n];
        Random rd = new Random();
        Integer index = 0;
        for (Integer i = 0; i < result.length; i++) {
            // 待选数组0到(len-2)随机一个下标
            index = Math.abs(rd.nextInt() % len--);
            // 将随机到的数放入结果集
            result[i] = source[index];
            // 将待选数组中被随机到的数，用待选数组(len-1)下标对应的数替换
            source[index] = source[len];
        }
        return result;
    }

    public static Integer[] concat(Integer[] a, Integer[] b) {
        if (a == null || b == null)
            return null;
        Integer[] c = new Integer[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    public static Integer[] randomArray(Integer[] xh) {
        List<Integer> list = Arrays.asList(xh);
        Collections.shuffle(list);
        xh = (Integer[]) list.toArray(new Integer[list.size()]);
        return xh;
    }

    public static String getRandomString() {
        String base = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 6; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static String getRandomNumber(int z) {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < z; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static String get64UUID() {
        String base = UUID.randomUUID().toString() + UUID.randomUUID().toString();
        return base.replace("-", "");
    }

    public static String get32UUID() {
        String base = UUID.randomUUID().toString();
        return base.replace("-", "");

    }
}
